package cn.iocoder.yudao.module.ems.service.maintaintask;

import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi;
import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOALeaveDO;
import cn.iocoder.yudao.module.bpm.dal.mysql.oa.BpmOALeaveMapper;
import cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceResultEnum;
import cn.iocoder.yudao.module.ems.controller.admin.maintaintask.vo.MaintainTaskItemCreateReqVO;
import cn.iocoder.yudao.module.ems.convert.maintaintask.MaintainTaskConvert;
import cn.iocoder.yudao.module.ems.convert.maintaintask.MaintainTaskItemConvert;
import cn.iocoder.yudao.module.ems.dal.dataobject.maintaintask.MaintainTaskDO;
import cn.iocoder.yudao.module.ems.dal.mysql.maintaintask.MaintainTaskItemMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.ems.controller.admin.maintaintask.vo.*;
import cn.iocoder.yudao.module.ems.dal.dataobject.maintaintask.MaintainTaskDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.ems.convert.maintaintask.MaintainTaskConvert;
import cn.iocoder.yudao.module.ems.dal.mysql.maintaintask.MaintainTaskMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.service.oa.BpmOALeaveServiceImpl.PROCESS_KEY;
import static cn.iocoder.yudao.module.ems.enums.ErrorCodeConstants.*;

/**
 * 保养任务 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class MaintainTaskServiceImpl implements MaintainTaskService {

    @Resource
    private BpmOALeaveMapper bpmMapper;

    @Resource
    private MaintainTaskMapper maintainTaskMapper;

    @Resource
    private MaintainTaskItemMapper maintainTaskItemMapper;

    @Resource
    private BpmProcessInstanceApi processInstanceApi;

    @Override
    public Long createMaintainTask(MaintainTaskCreateReqVO createReqVO) {
        // 插入
        MaintainTaskDO task = MaintainTaskConvert.INSTANCE.convert(createReqVO)
                .setResult(BpmProcessInstanceResultEnum.PROCESS.getResult());;
        maintainTaskMapper.insert(task);
        Collection<MaintainTaskItemCreateReqVO> itemList = createReqVO.getItem();
        for (MaintainTaskItemCreateReqVO item: itemList) {
            item.setTaskId(task.getId());
            maintainTaskItemMapper.insert(MaintainTaskItemConvert.INSTANCE.convert4(item));
        }

        // 发起 BPM 流程
        String processInstanceId = processInstanceApi.createProcessInstance(createReqVO.getUserId(),
                new BpmProcessInstanceCreateReqDTO().setProcessDefinitionKey(PROCESS_KEY)
                        .setBusinessKey(String.valueOf(task.getId())));

        // 将工作流的编号，更新到任务中
        bpmMapper.updateById(new BpmOALeaveDO().setId(task.getId()).setProcessInstanceId(processInstanceId));
        return task.getId();
    }

    @Override
    public void updateMaintainTask(MaintainTaskUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateMaintainTaskExists(updateReqVO.getId());
        // 更新
        MaintainTaskDO updateObj = MaintainTaskConvert.INSTANCE.convert(updateReqVO);
        maintainTaskMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaintainTask(Long id) {
        // 校验存在
        this.validateMaintainTaskExists(id);
        // 删除
        maintainTaskMapper.deleteById(id);
    }

    private void validateMaintainTaskExists(Long id) {
        if (maintainTaskMapper.selectById(id) == null) {
            throw exception(MAINTAIN_TASK_NOT_EXISTS);
        }
    }

    @Override
    public MaintainTaskDO getMaintainTask(Long id) {
        return maintainTaskMapper.selectById(id);
    }

    @Override
    public List<MaintainTaskDO> getMaintainTaskList(Collection<Long> ids) {
        return maintainTaskMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MaintainTaskDO> getMaintainTaskPage(MaintainTaskPageReqVO pageReqVO) {
        return maintainTaskMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MaintainTaskDO> getMaintainTaskList(MaintainTaskExportReqVO exportReqVO) {
        return maintainTaskMapper.selectList(exportReqVO);
    }

}
